package com.ts.api.module.api.controller;

import com.gitee.apanlh.annotation.viewresolver.RequestParamVo;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.exp.ChannelMarketParamException;
import com.ts.api.module.api.convert.core.annotation.ApiChannelMarketConvertType;
import com.ts.api.module.api.service.ApiChannelMarketService;
import com.ts.api.module.channel.entity.po.ChannelPO;
import com.ts.api.module.channel.service.ChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 *  H5-双向接入上游贷超撞库联登（采量）
 *
 *  @author Pan
 */
@RestController
@RequestMapping("/data/api/channel/market")
public class ApiChannelMarketUvController {

    @Autowired
    private ApiChannelMarketService apiChannelMarketService;
    @Autowired
    private ChannelService channelService;

    /**
     *  前置MD5撞库接口
     *
     *  @param  requestVO   请求实体
     *  @param  channelCode H5渠道编码
     *  @return Map
     */
    @RequestMapping("/exist/user/md5/{channelCode}")
    public Map<String, Object> existUserMd5(@RequestParamVo RequestVO requestVO, @PathVariable String channelCode) {
        ChannelPO channelPO = channelService.getByUniqueCode(channelCode);

        if (ValidParam.isNull(channelPO)) {
            throw new ChannelMarketParamException("非法渠道");
        }
        return apiChannelMarketService.existUserByMd5(channelPO, requestVO);
    }

    /**
     *  前置手机号8位掩码撞库接口
     *
     *  @param  requestVO   请求实体
     *  @param  channelCode H5渠道编码
     *  @return Map
     */
    @RequestMapping("/exist/user/mask/phone8/{channelCode}")
    public Map<String, Object> existUserPhoneMask8(@RequestParamVo RequestVO requestVO, @PathVariable String channelCode) {
        ChannelPO channelPO = channelService.getByUniqueCode(channelCode);

        if (ValidParam.isNull(channelPO)) {
            throw new ChannelMarketParamException("非法渠道");
        }
        return apiChannelMarketService.existUserPhoneMask(channelPO, requestVO, 8, ApiChannelMarketConvertType.DATA_EXCHANGE_PHONE_MASK_8);
    }

    /**
     *  前置手机号9位掩码撞库接口
     *
     *  @param  requestVO   请求实体
     *  @param  channelCode H5渠道编码
     *  @return Map
     */
    @RequestMapping("/exist/user/mask/phone9/{channelCode}")
    public Map<String, Object> existUserPhoneMask9(@RequestParamVo RequestVO requestVO, @PathVariable String channelCode) {
        ChannelPO channelPO = channelService.getByUniqueCode(channelCode);
        if (ValidParam.isNull(channelPO)) {
            throw new ChannelMarketParamException("非法渠道");
        }
        return apiChannelMarketService.existUserPhoneMask(channelPO, requestVO, 9, ApiChannelMarketConvertType.DATA_EXCHANGE_PHONE_MASK_9);
    }

    /**
     *  前置手机号10位掩码撞库接口
     *
     *  @param  requestVO   请求实体
     *  @param  channelCode H5渠道编码
     *  @return Map
     */
    @RequestMapping("/exist/user/mask/phone10/{channelCode}")
    public Map<String, Object> existUserPhoneMask10(@RequestParamVo RequestVO requestVO, @PathVariable String channelCode) {
        ChannelPO channelPO = channelService.getByUniqueCode(channelCode);

        if (ValidParam.isNull(channelPO)) {
            throw new ChannelMarketParamException("非法渠道");
        }
        return apiChannelMarketService.existUserPhoneMask(channelPO, requestVO, 10, ApiChannelMarketConvertType.DATA_EXCHANGE_PHONE_MASK_10);
    }

    /**
     *  用户资质撞库-目前只能接入对方
     *
     *  @param  requestVO   请求实体
     *  @param  channelCode H5渠道编码
     *  @return Map
     */
    @RequestMapping("/exist/user/form/{channelCode}")
    public Map<String, Object> dataExchangeForm(@RequestParamVo RequestVO requestVO, @PathVariable String channelCode) {
        ChannelPO channelPO = channelService.getByUniqueCode(channelCode);

        if (ValidParam.isNull(channelPO)) {
            throw new ChannelMarketParamException("非法渠道");
        }
        return apiChannelMarketService.existUserForm(channelPO, requestVO);
    }

    /**
     *  联登-手机号登录
     *
     *  @param  requestVO   请求实体
     *  @param  channelCode H5渠道编码
     *  @return Map
     */
    @RequestMapping("/federate/login/{channelCode}")
    public Map<String, Object> federateLogin(@RequestParamVo RequestVO requestVO, @PathVariable String channelCode) {
        ChannelPO channelPO = channelService.getByUniqueCode(channelCode);

        if (ValidParam.isNull(channelPO)) {
            throw new ChannelMarketParamException("非法渠道");
        }
        return apiChannelMarketService.federateLogin(channelPO, requestVO);
    }

    /**
     *  联登-用户资质登录-目前只能接入对方
     *
     *  @param  requestVO   请求实体
     *  @param  channelCode H5渠道编码
     *  @return Map
     */
    @RequestMapping("/federate/user/form/{channelCode}")
    public Map<String, Object> federateUserForm(@RequestParamVo RequestVO requestVO, @PathVariable String channelCode) {
        ChannelPO channelPO = channelService.getByUniqueCode(channelCode);

        if (ValidParam.isNull(channelPO)) {
            throw new ChannelMarketParamException("非法渠道");
        }
        return apiChannelMarketService.federateUserForm(channelPO, requestVO);
    }
}
